package cn.cxion.chat.app.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.cxion.chat.R;
import cn.cxion.chat.app.adapter.ConstantAdapter;
import cn.cxion.chat.app.bean.AppointmentInfo;
import cn.cxion.chat.app.bean.CheckContractBean;
import cn.cxion.chat.app.bean.CurrentBean;
import cn.cxion.chat.app.bean.MessageAction;
import cn.cxion.chat.app.bean.MessageEvent;
import cn.cxion.chat.app.bean.SecBean;
import cn.cxion.chat.app.constant.Constant;
import cn.cxion.chat.app.util.LogUtil;
import cn.cxion.chat.app.util.RealPathFromUriUtils;
import cn.cxion.chat.kit.Event;
import cn.cxion.chat.kit.WfcBaseActivity;
import cn.cxion.chat.kit.third.utils.ImageUtils;
import cn.cxion.chat.kit.utils.FileUtils;

public class ConstantActivity extends WfcBaseActivity {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.img_save)
    ImageView imgSave;
    @Bind(R.id.tv_title_right)
    TextView tvTitleRight;
    private CurrentBean currentBean;
    private ConstantAdapter adapter;
    private int classType;

    private List<SecBean> list = new ArrayList<>();

    private CheckContractBean checkContractBean;
    AppointmentInfo appointmentInfo;

    public static void show(Context context, int classKey) {
        Intent intent = new Intent(context, ConstantActivity.class);
        intent.putExtra("classKey", classKey);
        context.startActivity(intent);
    }

    public static void show(Context context, int classKey, CheckContractBean checkContractBean) {
        Intent intent = new Intent(context, ConstantActivity.class);
        intent.putExtra("classKey", classKey);
        intent.putExtra("checkContractBean", checkContractBean);
        context.startActivity(intent);
    }
 public static void show(Context context, int classKey, AppointmentInfo appointmentInfo) {
        Intent intent = new Intent(context, ConstantActivity.class);
        intent.putExtra("classKey", classKey);
        intent.putExtra("appointmentInfo", appointmentInfo);
        context.startActivity(intent);
    }

    @Override
    protected int contentLayout() {
        return R.layout.constant_activity;
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        int key = intent.getIntExtra("classKey", 0);
        checkContractBean = (CheckContractBean) intent.getSerializableExtra("checkContractBean");
        appointmentInfo = (AppointmentInfo) intent.getSerializableExtra("appointmentInfo");
        classType = key;
        currentBean = Constant.getClassBeanMap().get(key);
        setTitle(Constant.getClassKeyMap().get(key));
        rightTitleHold();
        init();
    }

    private void init() {
        adapter = new ConstantAdapter(this, currentBean.getSecList());
        adapter.setCheckContractBean(checkContractBean);
        adapter.setAppointmentInfo(appointmentInfo);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        if (currentBean.getClassId() == Constant.MYDATA || currentBean.getClassId() == Constant.COMPANYDATA)
            initMyData();
    }


    private void rightTitleHold() {
        if (classType == Constant.NICKNAAME || classType == Constant.SEX || classType == Constant.PHONE || classType == Constant.EMAIL || classType == Constant.POST || classType == Constant.ADDRESS) {
            imgSave.setVisibility(View.VISIBLE);
        } else {
            imgSave.setVisibility(View.GONE);
        }
        if (classType == Constant.DEMANDDETAILS||classType == Constant.CONTRACTCREATION) {
            if (checkContractBean != null)
                return;
            tvTitleRight.setVisibility(View.VISIBLE);
        } else if (classType == Constant.CLAIM) {
            tvTitleRight.setVisibility(View.VISIBLE);
            tvTitleRight.setText("认领");
        }else if (classType == Constant.CONTRACTDETAILS) {
            if (checkContractBean!=null&&checkContractBean.getCheckContractDeatail()!=null)
                return;
            tvTitleRight.setVisibility(View.VISIBLE);
            tvTitleRight.setText("发送");
        }else if (classType == Constant.APPROVE) {
            if (checkContractBean!=null&&checkContractBean.getApproveDetail()!=null)
                return;
            tvTitleRight.setVisibility(View.VISIBLE);
            tvTitleRight.setText("发送");
        }else if (classType == Constant.CUSTOMERAPPROVE) {
            if (checkContractBean!=null&&checkContractBean.getCustomerApproveDetail()!=null)
                return;
            tvTitleRight.setVisibility(View.VISIBLE);
            tvTitleRight.setText("发送");
        }else if (classType == Constant.ADDSCHEDULE) {
            if (checkContractBean!=null&&checkContractBean.getCustomerApproveDetail()!=null)
                return;
            tvTitleRight.setVisibility(View.VISIBLE);
            tvTitleRight.setText("添加");
        } else {
            tvTitleRight.setVisibility(View.GONE);
        }
    }

    private void initMyData() {
        for (SecBean secBean : currentBean.getSecList()) {
            if (secBean.getClassId() == Constant.VOICECALL) {
                secBean.setRightText("0分钟");
            } else {
                secBean.setRightText("0份");
            }
        }
        adapter.setSecList(currentBean.getSecList());
        adapter.notifyDataSetChanged();
    }


    @OnClick({R.id.img_save, R.id.tv_title_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_save:
                finish();
                break;
            case R.id.tv_title_right:
                adapter.rightClick();
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            Uri uri = data.getData();
            String path = FileUtils.getPath(this, uri);
            if (TextUtils.isEmpty(path)) {
                Toast.makeText(this, "选择文件错误", Toast.LENGTH_SHORT).show();
                return;
            }
            String type = path.substring(path.lastIndexOf("."));
            File file = new File(path);
            String[] str = Uri.decode(data.getDataString()).split("/");
            switch (type) {
                case ".txt":
                case ".doc":
                case ".docx":
                    EventBus.getDefault().post(new MessageEvent(MessageAction.SendFileName, str[str.length - 1]));
                    EventBus.getDefault().post(new MessageEvent(MessageAction.SendFilePath, FileUtils.getFileFromContentUri(uri, this).getPath()));
                    break;
                default:
                    EventBus.getDefault().post(new MessageEvent(MessageAction.SendFileName, file.getName()));
                    EventBus.getDefault().post(new MessageEvent(MessageAction.SendFilePath, path));
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().post(new MessageEvent(MessageAction.CheckContractUnregister, ""));
        super.onDestroy();
    }
}
